<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="行政区划" prop="adminCode">
        <el-cascader
          :options="adminCodeList"
          v-model="selectedOptions"
          :props ="adminCodeDESC"
          filterable
          change-on-select
          expand-trigger="hover"
          @change="handleChange">
        </el-cascader>
      </el-form-item>
      <el-row :gutter="24">
        <el-col :span="10">
          <el-form-item label="姓名" prop="extraName">
            <el-input v-model="dataForm.extraName" placeholder="姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="身份证号" prop="id">
            <el-input v-model="dataForm.id" placeholder="身份证号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="10">
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="微信号" prop="wechatId">
            <el-input v-model="dataForm.wechatId" placeholder="微信号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="10">
          <el-form-item label="开户行" prop="bank">
            <el-input v-model="dataForm.bank" placeholder="开户行"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="银行卡号" prop="cardNum">
            <el-input v-model="dataForm.cardNum" placeholder="银行卡号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-form-item label="身份证" prop="idPic">
          <el-col :span="10">
            <el-upload
              :action="url"
              :before-upload="beforeUploadHandle"
              :on-success="successHandle"
              :file-list="fileList"
              :show-file-list="false">
              <div v-if="dataForm.idImageA === ''">
                <img src="~@/assets/img/idImageA.png" @click="uploadHandle('idImageA')"/>
              </div>
              <div v-else>
                <img v-bind:src="dataForm.idImageA" @click="uploadHandle('idImageA')" style="width: 100%;"/>
              </div>
            </el-upload>
          </el-col>
          <el-col :span="10">
            <el-upload
              :action="url"
              :before-upload="beforeUploadHandle"
              :on-success="successHandle"
              :file-list="fileList"
              :show-file-list="false">
              <div v-if="dataForm.idImageB === ''">
                <img src="~@/assets/img/idImageB.png" @click="uploadHandle('idImageB')"  style="width: 100%;"/>
              </div>
              <div v-else>
                <img v-bind:src="dataForm.idImageB"   @click="uploadHandle('idImageB')" style="width: 100%;"/>
              </div>
            </el-upload>
          </el-col>
        </el-form-item>
      </el-row>
      <el-form-item label="" prop="idImageA" style="display: none">
        <el-input v-model="dataForm.idImageA" placeholder="身份证正面图片地址" type="hidden" ></el-input>
      </el-form-item>
      <el-form-item label="" prop="idImageB" style="display: none">
        <el-input v-model="dataForm.idImageB" placeholder="身份证背面图片地址"  type="hidden" ></el-input>
      </el-form-item>
      <el-input v-model="dataForm.adminCode" type="hidden"  placeholder="行政区划"></el-input>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        url: '',
        num: 0,
        successNum: 0,
        fileList: [],
        uploadFiled: '',
        dataForm: {
          userExtraId: 0,
          userId: '',
          extraName: '',
          male: '',
          id: '',
          mobile: '',
          wechatId: '',
          idImageA: '',
          idImageB: '',
          bank: '',
          cardNum: '',
          auditStatus: '',
          adminCode: '',
          createTime: '',
          updateTime: ''
        },
        selectedOptions: [],
        adminCodeList: [],
        adminCodeDESC: {value: 'adminCode',label: 'shortName',children: 'children'},
        dataRule: {
          userId: [
            {required: true, message: '账户编号不能为空', trigger: 'blur'}
          ],
          extraName: [
            {required: true, message: '姓名不能为空', trigger: 'blur'}
          ],
          male: [
            {required: true, message: '性别不能为空', trigger: 'blur'}
          ],
          id: [
            {required: true, message: '身份证号不能为空', trigger: 'blur'}
          ],
          mobile: [
            {required: true, message: '手机号不能为空', trigger: 'blur'}
          ],
          wechatId: [
            {required: true, message: '微信号不能为空', trigger: 'blur'}
          ],
          /*idImageA: [
            {required: true, message: '身份证正面图片地址不能为空', trigger: 'blur'}
          ],
          idImageB: [
            {required: true, message: '身份证背面图片地址不能为空', trigger: 'blur'}
          ],*/
          bank: [
            {required: true, message: '开户行不能为空', trigger: 'blur'}
          ],
          cardNum: [
            {required: true, message: '银行卡号不能为空', trigger: 'blur'}
          ],
          auditStatus: [
            {required: true, message: '人员信息审核状态不能为空', trigger: 'blur'}
          ],
          createTime: [
            {required: true, message: '创建时间不能为空', trigger: 'blur'}
          ],
          adminCode: [
            {required: true, message: '行政区划不能为空', trigger: 'blur'}
          ],
          updateTime: [
            {required: true, message: '更新时间不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.url = this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`)
        this.dataForm.userExtraId = id || 0
        this.visible = true
        this.getAdminCodes()
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userExtraId) {
            this.$http({
              url: this.$http.adornUrl(`/quickService/userExtra/info/${this.dataForm.userExtraId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.userExtra.userId
                this.dataForm.extraName = data.userExtra.extraName
                this.dataForm.male = data.userExtra.male
                this.dataForm.id = data.userExtra.id
                this.dataForm.mobile = data.userExtra.mobile
                this.dataForm.wechatId = data.userExtra.wechatId
                this.dataForm.idImageA = data.userExtra.idImageA
                this.dataForm.idImageB = data.userExtra.idImageB
                this.dataForm.bank = data.userExtra.bank
                this.dataForm.cardNum = data.userExtra.cardNum
                this.dataForm.auditStatus = data.userExtra.auditStatus
                this.dataForm.adminCode = data.userExtra.adminCode
                this.dataForm.createTime = data.userExtra.createTime
                this.dataForm.updateTime = data.userExtra.updateTime
                this.selectedOptions = data.adminCodes
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/quickService/userExtra/${!this.dataForm.userExtraId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userExtraId': this.dataForm.userExtraId || undefined,
                'userId': this.dataForm.userId,
                'extraName': this.dataForm.extraName,
                'male': this.dataForm.male,
                'id': this.dataForm.id,
                'mobile': this.dataForm.mobile,
                'wechatId': this.dataForm.wechatId,
                'idImageA': this.dataForm.idImageA,
                'idImageB': this.dataForm.idImageB,
                'bank': this.dataForm.bank,
                'cardNum': this.dataForm.cardNum,
                'auditStatus': this.dataForm.auditStatus,
                'createTime': this.dataForm.createTime,
                'adminCode': this.dataForm.adminCode,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.fileList = []
                    this.$emit('refreshDataList')

                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getAdminCodes () {
        this.$http({
          url: this.$http.adornUrl(`/quickService/areacode/adminCodeList`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.adminCodeList = data.adminCodeList
          }
        })
      },
      handleChange (data) {
        this.dataForm.adminCode = data[data.length-1]
      },
      uploadHandle(file) {
        this.uploadFiled = file;
      },
      // 上传之前
      beforeUploadHandle(file) {
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        this.num++
      },
      // 上传成功
      successHandle(response, file, fileList) {
        this.fileList = fileList
        this.successNum++
        if (response && response.code === 0) {
          if (this.num === this.successNum) {
            if(this.uploadFiled === "idImageA"){
              this.dataForm.idImageA = response.url
            }else if(this.uploadFiled === "idImageB"){
              this.dataForm.idImageB = response.url
            }
          }
        } else {
          this.$message.error(response.msg)
        }
        this.uploadFiled = ''
      }
    }
  }
</script>
